package top.jiangqiang.qyblog.core.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.jiangqiang.qyblog.common.base.query.PageResult;
import top.jiangqiang.qyblog.common.base.response.Result;
import top.jiangqiang.qyblog.core.base.service.impl.BaseServiceImpl;
import top.jiangqiang.qyblog.core.domain.dto.DraftQueryDto;
import top.jiangqiang.qyblog.core.domain.dto.DraftSaveOrUpdateDto;
import top.jiangqiang.qyblog.core.domain.entity.Draft;
import top.jiangqiang.qyblog.core.domain.entity.File;
import top.jiangqiang.qyblog.core.domain.vo.DraftVo;
import top.jiangqiang.qyblog.core.mapper.DraftMapper;
import top.jiangqiang.qyblog.core.service.IDraftService;

/**
 * <p>
 * 文章草稿 服务实现类
 * </p>
 *
 * @author JiangQiang
 * @since 2023-07-23
 */
@Service
public class DraftServiceImpl extends BaseServiceImpl<DraftMapper, Draft> implements IDraftService {

    @Override
    public Result<DraftVo> getDraftById(Long id) {
        return Result.ok(getById(id, DraftVo.class));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<DraftVo> saveOrUpdateDraft(DraftSaveOrUpdateDto draftSaveOrUpdateDto) {
        Draft draft = typeConverter.convert(draftSaveOrUpdateDto, Draft.class);
        saveOrUpdate(draft);
        return getDraftById(draft.getId());
    }

    @Override
    public Boolean deleteDraftById(Long id) {
        return removeById(id);
    }

    @Override
    public PageResult<DraftVo> findByCondition(DraftQueryDto query) {
        return lambdaJoinWrapper()
                .orderByDesc(File::getUpdateTime).page(query.toPage(), DraftVo.class);
    }
}
